home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / AppleSearch / Macintosh / ASClient Update SDK / UpdateSDK / UpdateMaker / UpdateMaker.cp < prev    next >
Encoding:
Text File  |  1995-01-19  |  10.2 KB  |  400 lines  |  [TEXT/MPS ]

  1. //_______________________________________________________________________________________________________________
  2. //___________________________________________________ INCLUDES __________________________________________________
  3. //_______________________________________________________________________________________________________________
  4.  
  5. #include <Values.h>
  6. #include <Types.h>
  7. #include <CType.h>
  8. #include <Resources.h>
  9. #include <Menus.h>
  10. #include <Fonts.h>
  11. #include <Events.h>
  12. #include <TextEdit.h>
  13. #include <Windows.h>
  14. #include <Memory.h>
  15. #include <Dialogs.h>
  16. #include <Desk.h>
  17. #include <ToolUtils.h>
  18. #include <OSEvents.h>
  19. #include <Balloons.h>
  20. #include <Errors.h>
  21. #include <StandardFile.h>
  22. #include <String.h>
  23. #include <Strings.h>
  24. #include <Shutdown.h>
  25. #include <GestaltEqu.h>
  26. #include <stdio.h>
  27. #include <StdLib.h>                
  28. #include <ErrMgr.h>
  29. #include <Errors.h>
  30. #include <Files.h>
  31. #include <Packages.h>
  32.  
  33. #include "ASUpdateArticleUtilities.h"
  34. #include "ASUpdateFileUtilities.h"
  35. #include "UpdateMaker.h"
  36.  
  37. //_______________________________________________________________________________________________________________
  38. //_________________________________________________ PROTOTYPING _________________________________________________
  39. //_______________________________________________________________________________________________________________
  40.  
  41. void             Initialize( void );
  42. void             InitializeMenus( void );
  43. int             main();
  44. void            DoEventLoop( void );
  45. void            DoMouseDown( EventRecord* evt );
  46. void             DoKeyDown( EventRecord* evt );
  47. void             DoMenuCommand( long menuResult );
  48.  
  49. OSErr             DoNew( void );
  50. Boolean         MakeNewspaperFile(short* theNPRefNum);
  51. Boolean         AddArticleFromFile(short theNPRefNum);
  52.  
  53. Handle            GetDIHandle( short item, DialogPtr dialog = nil );
  54. void             GetDIText( short item, Str255& str, DialogPtr dialog );
  55.  
  56. extern "C"         _DataInit( void );
  57.  
  58. //_______________________________________________________________________________________________________________
  59. //______________________________________________ IMPLEMENTATION _________________________________________________
  60. //_______________________________________________________________________________________________________________
  61.  
  62. #pragma segment Initialize
  63.  
  64. void Initialize( void )
  65. {
  66.     UnloadSeg( _DataInit );                                            // clear init segment out of heap
  67.     for ( short counter = 1; counter < 6; counter++ )                // this should give enough handles
  68.         MoreMasters();
  69.     InitGraf( ( Ptr )&qd.thePort );                                    // required stuff
  70.     InitFonts();
  71.     InitWindows();
  72.     InitMenus();
  73.     TEInit();
  74.     InitDialogs( nil );
  75.     InitCursor();
  76.     MaxApplZone();
  77.  
  78.     InitializeMenus();                                                // load MENU res and draw
  79. }
  80.  
  81. //_______________________________________________________________________________________________________________
  82.  
  83. void InitializeMenus( void )
  84. {        
  85.     MenuHandle    menu;
  86.     
  87.     AddResMenu( menu = GetMenu( mApple ), 'DRVR' );                     // get list of DAs into apple menu
  88.     InsertMenu( menu, 0 );    
  89.     InsertMenu( GetMenu( mFile ), 0 );    
  90.     InsertMenu( GetMenu( mEdit ), 0 );
  91.  
  92.     DrawMenuBar();
  93. }
  94.  
  95. //_______________________________________________________________________________________________________________
  96.  
  97. #pragma segment main
  98.  
  99. int main()
  100. {
  101.     Initialize();                                                // do startup things
  102.     UnloadSeg( Initialize );                                    // we won't need that code ever again
  103.     DoEventLoop();
  104.     return 0;                                                    // dedicated ANSI standard follower
  105. }
  106.  
  107. //_______________________________________________________________________________________________________________
  108.  
  109. void DoEventLoop( void )
  110. {
  111.     EventRecord        evt;
  112.     
  113.     for (;;) {    
  114.         if ( WaitNextEvent( everyEvent, &evt, kSleepValue, ( RgnHandle )nil ) ) {
  115.             switch (evt.what) {
  116.                 case mouseDown:
  117.                         DoMouseDown( &evt );
  118.                         break;
  119.                 case keyDown:
  120.                         DoKeyDown( &evt );
  121.                         break;
  122.                 default:                                        // we don't care about other events
  123.                         break;
  124.             }
  125.         }
  126.     }
  127. }
  128.  
  129. //_______________________________________________________________________________________________________________
  130.  
  131. void DoMouseDown( EventRecord* evt )
  132. {
  133.     WindowPtr    window;
  134.     
  135.     short part = FindWindow( evt->where, &window );
  136.     switch ( part ) {                                            // figure out where it was clicked
  137.         case inMenuBar:    
  138.                 DoMenuCommand( MenuSelect( evt->where ) );
  139.                 break;
  140.         case inDrag:                                            // window's title bar
  141.         case inGoAway:                                            // window's close box
  142.         case inContent:                                            // inside window
  143.         case inGrow:
  144.                 break;
  145.         case inSysWindow:                                        // it's a DA window
  146.                 SystemClick( evt, window );
  147.                 break;
  148.     }
  149. }
  150.  
  151. //_______________________________________________________________________________________________________________
  152.  
  153. void DoKeyDown( EventRecord* event )
  154. {
  155.     if ( event->modifiers & cmdKey )                        // a command key
  156.         DoMenuCommand( MenuKey( ( char )event->message & charCodeMask ) );
  157. }
  158.  
  159. //_______________________________________________________________________________________________________________
  160.  
  161. void DoMenuCommand( long menuResult )
  162. {
  163.     Str255        daName;
  164.  
  165.     short menuItem = LoWord( menuResult );    
  166.     switch ( HiWord( menuResult ) ) {
  167.         case mApple:
  168.             switch ( menuItem ) {
  169.                 case iAbout:        
  170.                     SysBeep(20);
  171.                     break;
  172.                 default:            
  173.                     GetItem( GetMHandle( mApple ), menuItem, daName);
  174.                     short daRefNum = OpenDeskAcc( daName );
  175.                     break;
  176.             }
  177.             break;
  178.  
  179.         case mFile:
  180.             switch ( menuItem ) {
  181.                 case iNew:
  182.                     DoNew();
  183.                     break;
  184.                 case iQuit:        
  185.                     ExitToShell();                                        // change it to PostCommand( ID_QUIT )
  186.                     break;
  187.             }
  188.             break;
  189.  
  190.         default:                                                        // ignore Edit menu
  191.             break;
  192.     }
  193.     HiliteMenu(0);                    
  194. }
  195.  
  196. //_______________________________________________________________________________________________________________
  197.  
  198. OSErr DoNew( void )
  199. {
  200.     OSErr    err;
  201.     short    theNPRefNum;
  202.     Boolean    done = false;
  203.     
  204.     if (!MakeNewspaperFile(&theNPRefNum))
  205.         return noErr;
  206.         
  207.     while (!done) {
  208.         done = !AddArticleFromFile(theNPRefNum);
  209.     }
  210.         
  211.     err = ASUCloseUpdateFile(theNPRefNum);
  212.     return noErr;
  213. }
  214.  
  215. //_______________________________________________________________________________________________________________
  216.  
  217. Boolean MakeNewspaperFile(short* theNPRefNum)
  218. {
  219.     OSErr                err;
  220.     StandardFileReply    reply;
  221.  
  222.     StandardPutFile( "\pNewspaper name:", "\pIguana Daily", &reply );
  223.     if (!reply.sfGood)
  224.         return false;
  225.     if ((err = ASUCreateUpdateFile(&reply.sfFile, 'MRLW', kAppleSearchTextUpdate, kVersion1)) != noErr)
  226.         return false;
  227.     if ((err = ASUOpenUpdateFile(&reply.sfFile, theNPRefNum)) != noErr)
  228.         return false;
  229.     
  230.     return true;
  231. }
  232.  
  233. //_______________________________________________________________________________________________________________
  234.  
  235. Boolean AddArticleFromFile(short theNPRefNum)
  236. {
  237.     enum {maxArticleSize = 8096};
  238.     
  239.     OSErr                err;
  240.     StandardFileReply    reply;
  241.     SFTypeList            types;
  242.     
  243.     ASUArticleHeader    theArticleInfo;
  244.     ASUDataSize            count;
  245.     ASUDCPtr            dc;
  246.     Ptr                    buffer;
  247.     short                theSrcRefnum;
  248.     long                srcEOF;
  249.     long                readSize;
  250.     
  251.     char                title[40];
  252.     
  253.     static long        theArticleNumber = 1;
  254.     
  255.     // Open a source file
  256.     types[0] = 'TEXT';
  257.     StandardGetFile(NULL, 1, types, &reply);
  258.     if (!reply.sfGood)
  259.         return false;
  260.     if ((err = FSpOpenDF(&reply.sfFile, fsRdPerm, &theSrcRefnum)) != noErr)
  261.         return false;
  262.     
  263.     // Get file size & allocate buffer
  264.     if ((err = GetEOF(theSrcRefnum, &srcEOF)) != noErr) {
  265.         FSClose(theSrcRefnum);
  266.         return false;
  267.     }
  268.     buffer = NewPtr(srcEOF);
  269.     if (buffer == NULL) {
  270.         FSClose(theSrcRefnum);
  271.         return false;
  272.     }
  273.     
  274.     {
  275.         DateTimeRec            DateTime;
  276.         CInfoPBPtr pb = (CInfoPBPtr)NewPtrClear( sizeof ( CInfoPBRec ) );
  277.         pb->hFileInfo.ioNamePtr = &reply.sfFile.name[0];
  278.         pb->hFileInfo.ioVRefNum = reply.sfFile.vRefNum;
  279.         pb->hFileInfo.ioDirID = reply.sfFile.parID;
  280.         
  281.         if ( ( err = PBGetCatInfo( pb, false ) ) != noErr ) {
  282.             debugstr( "PBGetCatInfo error" );
  283.             return false;
  284.         }
  285.         else {
  286.             Secs2Date( pb->hFileInfo.ioFlMdDat, &DateTime );
  287.             BlockMove(&DateTime, &theArticleInfo.articleDateTime, sizeof(ASUArticleDate));
  288.         }
  289.         
  290.         if ( pb ) {
  291.             DisposePtr( (Ptr)pb );
  292.             pb = nil;
  293.         }
  294.     }
  295.     
  296.     // Read file into buffer
  297.     readSize = srcEOF;
  298.     if ((err = FSRead(theSrcRefnum, &readSize, buffer)) != noErr) {
  299.         debugstr("FSRead error");
  300.         FSClose(theSrcRefnum);
  301.         return false;
  302.     }
  303.     if ((err = FSClose(theSrcRefnum)) != noErr) {
  304.         debugstr("FSRead error");
  305.         return false;
  306.     }
  307.     if (readSize != srcEOF) {
  308.         debugstr("readSize != srcEOF");
  309.         return false;
  310.     }
  311.     
  312.     
  313.     // Build the article info
  314.     theArticleInfo.articleDataSize = readSize;
  315.     theArticleInfo.articleType = kTextArticleItem;
  316.         
  317.     theArticleInfo.userBytes = theArticleNumber;
  318.     
  319.     
  320.     BlockMove( (Ptr)&reply.sfFile.name[0], (Ptr)title, reply.sfFile.name[0] + 1 );
  321.     p2cstr( (Str255)title );
  322.     
  323.     theArticleInfo.articleTitle = title;
  324.     
  325.     // prompt user for the source name
  326.     {
  327.         DialogPtr dialog = GetNewDialog( rSourceTextEditDialogID, nil, (WindowPtr) -1 );
  328.         if ( dialog ) {
  329.             short         item = 0;
  330.             WindowPtr    saved = nil;
  331.             Str255        source;
  332.             
  333.             GetPort( &saved );
  334.             SetPort( dialog );
  335.             ShowWindow( dialog );
  336.             SelectWindow( dialog );
  337.  
  338.             do {
  339.                 ModalDialog( nil, &item );                    // Call DM routine without filter proc
  340.             } while ( item != diOKItem );                    // Until OK button is clicked
  341.             
  342.             GetDIText( diSourceTextEditItem, source, dialog );
  343.             if ( source[0] ) {
  344.                 theArticleInfo.articleSource = NewPtrClear( source[0] + 1 );    // +1 for null terminator
  345.                 BlockMove( (Ptr)&source[1], theArticleInfo.articleSource, source[0] );
  346.             }
  347.             else {
  348.                 theArticleInfo.articleSource = nil;
  349.             }
  350.             
  351.             SetPort( saved );                                // Change back the grafPort
  352.             HideWindow( dialog );
  353.             DisposeDialog( dialog );                
  354.         }
  355.         else {
  356.             return false;
  357.         }
  358.     }
  359.     
  360.     count = theArticleInfo.articleDataSize;
  361.     
  362.     if ((err = ASUAddArticle(theNPRefNum, &theArticleInfo, count, buffer, &dc)) != noErr)
  363.         return false;
  364.     
  365.     if ( buffer ) {
  366.         DisposePtr( buffer );
  367.         buffer = nil;
  368.     }
  369.         
  370.     return true;
  371. }
  372.  
  373. //_______________________________________________________________________________________________________________
  374.  
  375. Handle GetDIHandle( short item, DialogPtr dialog )
  376. {
  377.     short type;
  378.     Handle handle;
  379.     Rect box;
  380.  
  381.     if ( dialog == nil )
  382.         dialog = (DialogPtr)FrontWindow();
  383.     GetDItem( dialog, item, &type, &handle, &box );
  384.     return handle;
  385. }
  386.     
  387. //_______________________________________________________________________________________________________________
  388.  
  389. void GetDIText( short item, Str255& str, DialogPtr dialog )
  390. {
  391.     Handle handle = GetDIHandle( item, dialog );
  392.     GetIText( handle, str );
  393. }
  394.  
  395. //_______________________________________________________________________________________________________________
  396.  
  397.  
  398.  
  399.  
  400.